// jTranslate.js
// A wrapper for GTranslate API v2.
// Since the dev team apparently won't do it themselves.

(function(){
 function translate(srcText, srcLang, destLang, callback){
  if(!window['translateAPIkey']){ // check to make sure we have an api key
   throw('Unable to translate text: no API key found.');
  }
  if(!window['jeremy']['language']['callbacks']){
   window['jeremy']['language']['callbacks']=[];
  }
  var callbacksArray = window['jeremy']['language']['callbacks'],
   params = { // process the args
    'key' : window['translateAPIkey'],
    'source' : srcLang,
    'target' : destLang,
    'q' : typeof(srcText)=='string' ? srcText : srcText['text'],
    'callback' : 'window.jeremy.language.callbacks['+callbacksArray.length+']',
    'format' : typeof(srcText)=='object' ? srcText['type'] : null
   },
   args = [];
  callbacksArray.push(callback);
  for(var i in params){
   if(params[i]){
    args.push([i,encodeURIComponent(params[i])].join('='));
   }
  }
  if(encodeURIComponent(srcText).length > 1000 && window['translateProxyUrl']){ // Check if we need AND have a server-side proxy to relay long strings for us
   var xhr;
   if(window.XMLHttpRequest){
    xhr = new window.XMLHttpRequest;
   }else{
    try{
     xhr = new ActiveXObject('Msxml2.XMLHTTP');
    }catch(err){
     try{
      xhr = new ActiveXObject('Microsoft.XMLHTTP');
     }catch(err2){
     }
    }
   }
   if(xhr){
    xhr.open('POST',window['translateProxyUrl'],true);
    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xhr.setRequestHeader('Content-length',args.length);
    xhr.onreadystatechange = function(){
     if(xhr.readyState == 4 && xhr.status == 200){
      try{
       eval(xhr.responseText);
      }catch(err){
       throw('Unable to process response: '+xhr.readyState);
      }
     }
    };
    xhr.send(args.join('&'));
    return;
   }
  }
  // If there is no proxy involved, then we fall back to JSONP.
  var script = createEl('script',null,{type:'text/javascript'}),
   serviceUrl = 'https://www.googleapis.com/language/translate/v2?',
   onload = function(){
    script.onload = null;
    script.parentNode.removeChild(script);
    delete(script)
   },
   onreadystatechange = function(){
    if(script.readystate == 'complete' || script.readystate == 'ready'){
     script.onreadystatechange = null;
     onload();
    }
   };
  script.src = serviceUrl + args.join('&');
  if(navigator.product=='Gecko'){
   script.onload = onload;
  }else{
   script.onreadystatechange = onreadystatechange;
  }
  document.getElementsByTagName('head')[0].appendChild(script);
 }
 
 function TranslateControl(el, opt_options){
  this.container = typeof(el)=='string' ? document.getElementById(el) : el;
  this.options = opt_options;
 }
 
 // utility functions
 function createEl(tagName, children, attribs){ // create elements
  var el = document.createElement(tagName || 'div');
  if(children){
   for(var i=0;i<children.length;i++){
    try{
     el.appendChild(children[i]);
    }catch(err){}
   }
  }
  if(attribs){
   for(var i in attribs){
    el.setAttribute(i, attribs[i]);
   }
  }
  return el;
 }
 
 // export stuff
 if(!window['jeremy']){
  window['jeremy'] = {};
 }
 window['jeremy']['language'] = {
  'translate' : translate
 };
})()